package cn.iocoder.mall.user.biz.service.impl;

import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.utils.common.api.vo.AppMessageTOMQDTO;
import cn.iocoder.common.utils.common.util.ObjectUtils;
import cn.iocoder.mall.user.api.AppMessageService;
import cn.iocoder.mall.user.api.dto.AppMessageDTO;
import cn.iocoder.mall.user.api.dto.AppNoticeDTO;
import cn.iocoder.mall.user.biz.dao.AppMessageMapper;
import cn.iocoder.mall.user.biz.dao.AppNoticeMapper;
import cn.iocoder.mall.user.biz.dataobject.AppMessage;
import cn.iocoder.mall.user.biz.dataobject.AppNotice;
import cn.iocoder.mall.user.biz.service.IMemberService;
import cn.iocoder.mall.user.biz.service.ISellerService;
import cn.iocoder.mall.user.biz.untils.GeneratorSnowFlakeIDUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.Date;

import static cn.iocoder.common.framework.constant.CommonConstant.SMS_ROCKET_APPMESSAGE;

/***
 *@author yinxucun
 *@date 2019-12-03 16:59 
 */
@Service
@org.apache.dubbo.config.annotation.Service(validation = "true",version = "${dubbo.provider.AppMessageService.version}")
public class AppMessageImpl implements AppMessageService {

    //TODO 需要加上短信编码，以及构造短信内容参数


    @Autowired
    private GeneratorSnowFlakeIDUtils generatorSnowFlakeIDUtils;

    @Resource
    AppMessageMapper appMessageMapper;
    @Resource
    AppNoticeMapper appNoticeMapper;
    @Resource
    RocketMQTemplate rocketMQTemplate;
    @Autowired
    private IMemberService memberService;

    @Autowired
    private ISellerService sellerService;

    @Override
    public CommonResult sendAppMessageToUser(AppMessageDTO dto)  {
        if (messageDTOParamsIsNull(dto)){
            return CommonResult.error("参数不足");
        }
        switch (dto.getType()){
            case 101:
                sendUpdatePasswordAndMobileMessage(dto);
                break;
            case 201:
                 sendPaySuccessMessage(dto);
                 break;
            case 301:
                sendWriterOffOrderMessage(dto);
                break;
            case 107:
                sendWriterMemberNotificationMessage(dto);
                break;
            case 108:
                sendWriterPaymentNoticeMessage(dto);
                break;
            case 109:
                sendWriterOrderNotificationMessage(dto);
                break;
            case 110:
                sendWriterMerchantStoreNoticeMessage(dto);
                break;
            case 111:
                sendWriterVersionNotificationMessage(dto);
                break;
            case 112:
                sendWriterActivityNoticeMessage(dto);
                break;
            case 113:
                sendWriterCommoditynNotificationMessage(dto);
                break;
            case 114:
                sendWriterVerificationCode(dto);
                break;
            case 115:
                sendWriterPromotersAddShopsMessage(dto);
                break;
            case 401:
                sendOrderPaySuccessToSeller(dto);
                break;
            default: break;
        }

        return null;
    }

    @Override
    public CommonResult sendAppMessageToAll(AppNoticeDTO dto)  {
        AppNotice appNotice=ObjectUtils.copyObject(dto, AppNotice.class);

        appNotice.setUpdateTime(new Date());
        appNoticeMapper.insert(appNotice);
        appNoticeMapper.saveLog(appNotice.getId(),dto.getUserId(), LocalDateTime.now());
        return CommonResult.ok();
    }

    /**
     * 发送验证码
     * @param dto
     */
    private void sendWriterVerificationCode(AppMessageDTO dto) {
        rocketMQTemplate.convertAndSend(SMS_ROCKET_APPMESSAGE,new AppMessageTOMQDTO().setContent(dto.getContent())
                .setMobile(getMobile(dto.getReceiver(),1)));
    }

    /**
     * 商城活动类通知
     * @param dto
     */
    private boolean sendWriterActivityNoticeMessage(AppMessageDTO dto) {
        AppMessage message=ObjectUtils.copyObject(dto,AppMessage.class);
        initAppMessage(message);
        return  appMessageMapper.insert(message)>0;

    }
    /**
     * 系统版本通知
     * @param dto
     */
    private boolean sendWriterVersionNotificationMessage(AppMessageDTO dto) {
        AppMessage message=ObjectUtils.copyObject(dto,AppMessage.class);
        initAppMessage(message);
        return  appMessageMapper.insert(message)>0;
    }
    /**
     * 商户店铺消息通知
     * @param dto
     */
    private boolean sendWriterMerchantStoreNoticeMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 2,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 订单类消息通知
     * @param dto
     */
    private boolean sendWriterOrderNotificationMessage(AppMessageDTO dto) {
        AppMessage message=ObjectUtils.copyObject(dto,AppMessage.class);
        initAppMessage(message);
        return  appMessageMapper.insert(message)>0;
    }

    /**
     * 支付类消息通知
     * @param dto
     */
    private boolean sendWriterPaymentNoticeMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 会员通知
     * @param dto
     */
    private boolean sendWriterMemberNotificationMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 商品通知
     * @param dto
     * @return
     */
    private boolean sendWriterCommoditynNotificationMessage(AppMessageDTO dto) {
        AppMessage appMessage=new AppMessage();
        BeanUtils.copyProperties(dto,appMessage);
        initAppMessage(appMessage);
        return  appMessageMapper.insert(appMessage)>0;
    }

    /**
     * 推广员推广店铺
     * @param dto
     */
    private boolean sendWriterPromotersAddShopsMessage(AppMessageDTO dto) {
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    boolean sendOrderPaySuccessToSeller(AppMessageDTO dto) {
        //通过mq发送短信
        AppMessage appMessage = getAppMessage(dto, 1, "");
        return  appMessageMapper.insert(appMessage)>0;
    }

    boolean sendWriterOffOrderMessage(AppMessageDTO dto) {
        //支付成功
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    boolean sendUpdatePasswordAndMobileMessage(AppMessageDTO dto)  {

        AppMessage message=ObjectUtils.copyObject(dto,AppMessage.class);

        initAppMessage(message);
        return  appMessageMapper.insert(message)>0;

    }

    boolean sendPaySuccessMessage(AppMessageDTO dto){
        //支付成功
        AppMessage appMessage = getAppMessage(dto, 1,"");
        return  appMessageMapper.insert(appMessage)>0;
    }

    private String getMobile(String receiver,Integer type) {
        if (type==1){
            return memberService.getMobileById(receiver);

        }
        if (type==2){
            return sellerService.getMobileById(receiver);
        }
        return null;
    }

    private AppMessage getAppMessage(AppMessageDTO dto, Integer type, String templateCode){
        AppMessage message=ObjectUtils.copyObject(dto,AppMessage.class);
        rocketMQTemplate.convertAndSend(SMS_ROCKET_APPMESSAGE,new AppMessageTOMQDTO().setContent(dto.getContent())
                .setMobile(getMobile(dto.getReceiver(),type)).setTemplateCode(templateCode));
        initAppMessage(message);
        return message;
    }

    boolean messageDTOParamsIsNull(AppMessageDTO dto){
        return  ObjectUtils.isNull(dto.getCreator(),dto.getType(),dto.getReceiver(),dto.getContent());
    }
    void initAppMessage(AppMessage message){
        message.setIsRead(0);
        message.setIsDelete(0);
        message.setId(generatorSnowFlakeIDUtils.nextId());
    }
}
